"""
配置文件 - 统一管理API配置、代理设置和日志配置
"""
import os
import logging
from typing import Optional

class Config:
    """配置类 - 管理所有配置项"""
    
    def __init__(self):
        # API配置
        self.api_key = "sk-871418ac069d4ff0b9addaa11eef58e9"
        self.base_url = "https://api.deepseek.com/v1"
        self.model = "deepseek-chat"
        
        # 代理配置
        self.use_proxy = True  # 是否使用代理
        self.proxy_url = "http://127.0.0.1:7897"  # 代理地址
        
        # 网络配置
        self.timeout = 30  # 请求超时时间（秒）
        self.max_retries = 3  # 最大重试次数
        
        # 日志配置
        self.log_level = logging.INFO
        self.log_file = "co_driving_llm.log"
        
        # 环境配置
        self.environment = "development"  # development, production
        
        # 从环境变量读取配置（如果存在）
        self._load_from_env()
    
    def _load_from_env(self):
        """从环境变量加载配置"""
        self.api_key = os.getenv("DEEPSEEK_API_KEY", self.api_key)
        self.base_url = os.getenv("DEEPSEEK_BASE_URL", self.base_url)
        self.proxy_url = os.getenv("PROXY_URL", self.proxy_url)
        self.use_proxy = os.getenv("USE_PROXY", "true").lower() == "true"
        self.environment = os.getenv("ENVIRONMENT", self.environment)
    
    def get_proxy_config(self) -> Optional[dict]:
        """获取代理配置"""
        if self.use_proxy:
            return {
                "http://": self.proxy_url,
                "https://": self.proxy_url
            }
        return None
    
    def setup_logging(self):
        """设置日志配置"""
        logging.basicConfig(
            level=self.log_level,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
            handlers=[
                logging.FileHandler(self.log_file),
                logging.StreamHandler()
            ]
        )
        
        # 设置httpx日志级别
        logging.getLogger("httpx").setLevel(logging.WARNING)
        logging.getLogger("openai").setLevel(logging.WARNING)

# 全局配置实例
config = Config()

# 设置日志
config.setup_logging()
logger = logging.getLogger(__name__)

